package com.qiaotongtianxia.loans.fragments;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.view.Gravity;
import android.view.View;

import com.qiaotongtianxia.loans.R;
import com.qiaotongtianxia.loans.activity.CertificateActivity;
import com.qiaotongtianxia.loans.activity.LoansRecordActivity;
import com.qiaotongtianxia.loans.activity.RecommendActivity;
import com.qiaotongtianxia.loans.activity.RefundRecordActivity;
import com.qiaotongtianxia.loans.activity.ServiceActivity;
import com.qiaotongtianxia.loans.beanManager.MemberManager;
import com.qiaotongtianxia.loans.functionInterface.FragmentLogout;
import com.qiaotongtianxia.loans.functionInterface.IBaseRequestImp;
import com.qiaotongtianxia.loans.functionInterface.IWarningDialog;
import com.qiaotongtianxia.loans.net.Api;
import com.qiaotongtianxia.loans.util.CommonUtils;
import com.qiaotongtianxia.loans.util.ToastUtil;
import com.qiaotongtianxia.loans.views.BaseTextView;
import com.qiaotongtianxia.loans.views.DragLayout;
import com.qiaotongtianxia.loans.views.dialogs.DialogWarning;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lisen on 2017/12/15.
 *
 * @author lisen < 4533548588@qq.com >
 */

public class MeFragment extends BaseFragment implements DragLayout.DragListener{
    @BindView(R.id.tv_name)
    BaseTextView tvName;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.dragLayout)
    DragLayout dragLayout;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_me;
    }

    @Override
    public Api onApiCreate() {
        return new Api(context);
    }

    @Override
    public void created(View view) {

        String name = MemberManager.getMember(context).getNickname();
        tvName.setText(name);
        dragLayout.setListener(this);
    }

    @OnClick({R.id.tv_certificate, R.id.tv_loansRecord, R.id.tv_refundRecord,R.id.tv_recommend,
            R.id.tv_service, R.id.tv_logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_certificate:
                api.isCertificate(new IBaseRequestImp<Map<String, String>>() {
                    @Override
                    public void onRequestSuccess(Map<String, String> result) {
                        ToastUtil.showShort(context, result.get("msg"));
                        switch (CommonUtils.parseInt(result.get("auth_status"))) {
                            case 0:
                            case 3://去实名认证
                                Intent intent = new Intent(context, CertificateActivity.class);
                                startActivity(intent);
                                break;
                        }
                    }
                });
                break;
            case R.id.tv_loansRecord:
                Intent intentLoands = new Intent(context, LoansRecordActivity.class);
                startActivity(intentLoands);
                break;
            case R.id.tv_refundRecord:
                Intent intentReunfd = new Intent(context, RefundRecordActivity.class);
                startActivity(intentReunfd);
                break;
            case R.id.tv_recommend:
                Intent intentRecommend = new Intent(context, RecommendActivity.class);
                startActivity(intentRecommend);
                break;
            case R.id.tv_service:
                Intent intentService = new Intent(context, ServiceActivity.class);
                startActivity(intentService);
                break;
            case R.id.tv_logout:

                DialogWarning warning = new DialogWarning(context, new IWarningDialog() {
                    @Override
                    public void onConfirmListener() {
                        api.logout(new IBaseRequestImp<Nullable>() {
                            @Override
                            public void onRequestSuccess(Nullable nullable) {
                                ((FragmentLogout) fragmentListener).logout();
                            }
                        });
                    }

                    @Override
                    public void onCancelListener() {
                    }
                });
                warning.showTitle(View.GONE);
                warning.setMsgGravity(Gravity.CENTER);
                warning.show("确定要退出吗?");

                break;
        }
    }

    @Override
    public boolean isCanPullDown() {
        return CommonUtils.isReachTop(scrollView);
    }

    @Override
    public boolean isCanPullUp() {
        return CommonUtils.isReachBottom(scrollView);
    }
}
